package com.le.tester.tip;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * createTime：2022/2/11 16:41
 * description：reentrantLock公平锁
 */
public class ReentrantLockTester2 {
    private static final Lock LOCK = new ReentrantLock(false);

    public static void main(String[] args) {

        new Thread(ReentrantLockTester2::test, "线程A").start();
        new Thread(ReentrantLockTester2::test, "线程B").start();
        new Thread(ReentrantLockTester2::test, "线程C").start();
        new Thread(ReentrantLockTester2::test, "线程D").start();
        new Thread(ReentrantLockTester2::test, "线程E").start();

    }

    public static void test() {
        for (int i = 0; i < 2; i++) {
            LOCK.lock();
            try {
                System.out.println(Thread.currentThread().getName() + "获取到了锁");
                TimeUnit.MILLISECONDS.sleep(200);

            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                //System.out.println("不释放锁");
                LOCK.unlock();
            }
            System.out.println("================");
        }

    }
}
